package com.tsd.jxc.fin.service.impl;

import com.tsd.base.service.BaseCodeRuleService;
import com.tsd.core.constants.Constants;
import com.tsd.core.service.GetDuplicatedObj;
import com.tsd.core.thread.ThreadPoolTaskUtil;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.PageBean;
import com.tsd.jxc.config.JxcCodeRuleConstants;
import com.tsd.jxc.fin.dao.JxcFinanceFundDetailMapper;
import com.tsd.jxc.fin.entity.JxcFinanceFundDetail;
import com.tsd.jxc.fin.entity.JxcFinanceFundDetailExt;
import com.tsd.jxc.fin.service.JxcFinanceFundDetailService;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import com.tsd.system.entity.SysUserExt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 描述：JxcFinanceFundDetail 服务实现层
 *
 * @author Hillpool
 * @date 2023/02/09 18:00:21
 */
@Service
public class JxcFinanceFundDetailServiceImpl extends BaseServiceImpl implements JxcFinanceFundDetailService, GetDuplicatedObj {

    @Resource
    private JxcFinanceFundDetailMapper jxcFinanceFundDetailMapper;
    @Resource
    private BaseCodeRuleService baseCodeRuleService;

    @Override
    public JxcFinanceFundDetail getJxcFinanceFundDetailById(Long id) throws Exception {
        return jxcFinanceFundDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public JxcFinanceFundDetailExt queryBySid(String sid) throws Exception {
        return jxcFinanceFundDetailMapper.selectBySid(sid);
    }

    @Override
    public void deleteJxcFinanceFundDetails(List<String> list, SysUser opUser) throws Exception {
        if (list.isEmpty()) {
            return;
        }
        JxcFinanceFundDetailExt params = new JxcFinanceFundDetailExt();
        params.setDr(0);
        params.setSid_list(list);
        List<JxcFinanceFundDetailExt> detailExtList = jxcFinanceFundDetailMapper.selectByEntity(params);
        if (HlpUtils.isEmptyList(detailExtList)) {
            throw new HlpException("单据信息为空");
        }

        List<JxcFinanceFundDetailExt> filterList = ListUtil.filter(detailExtList, t -> Constants.VALUE_YES == t.getPay_req_state());
        if (!HlpUtils.isEmptyList(filterList)) {
            List<String> codeList = ListUtil.map(filterList, JxcFinanceFundDetailExt::getCode);
            throw new HlpException("单据：" + StringUtils.join(codeList, "、") + "已生成对账，无法删除");
        }

        List<JxcFinanceFundDetailExt> updateList = new ArrayList<>();
        for (JxcFinanceFundDetailExt detailExt : detailExtList) {
            super.autoInjectBaseData(detailExt, opUser, this.TYPE_DELETE);
            updateList.add(detailExt);
        }
        if (!HlpUtils.isEmptyList(updateList)) {
            ThreadPoolTaskUtil.executeInList4Split(updateList, false, itemList -> jxcFinanceFundDetailMapper.updateList(itemList));
        }
    }

    @Override
    public boolean isDuplicated(Object obj, String code) {
        if (obj == null) {
            return false;
        }
        JxcFinanceFundDetailExt orderExt = (JxcFinanceFundDetailExt) obj;
        orderExt.setCode(code);
        return jxcFinanceFundDetailMapper.selectDuplicatedCode(orderExt) != null;
    }

    @Override
    public void saveJxcFinanceFundDetail(JxcFinanceFundDetailExt record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.checkEmpty(record.getType(), "请提供参数：type");
            super.checkEmpty(record.getRelated_party_sid(), "请提供参数：related_party_sid");
            if (!HlpUtils.isEmpty(record.getCode())) {
                //如果是手动设置了编号，则检查是否有相同的，有的话，置空后重新生成
                Integer check = jxcFinanceFundDetailMapper.selectDuplicatedCode(record);
                if (check != null) {
                    record.setCode(null);
                }
            }
            String genCode = baseCodeRuleService.genCode(record.getRelated_party_sid(), JxcCodeRuleConstants.TYPE_JXC_FINANCE_RECV_PAY_BILL, JxcCodeRuleConstants.TYPE_NAME_JXC_FINANCE_RECV_PAY_BILL, opUser, record.getCode(), record, this);
            record.setCode(genCode);

            record.setAudit_state(Constants.VALUE_NO);
            record.setState(Constants.VALUE_YES);
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            jxcFinanceFundDetailMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            jxcFinanceFundDetailMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public List<JxcFinanceFundDetailExt> findJxcFinanceFundDetails(Map<String, Object> params, PageBean page) throws Exception {
        return jxcFinanceFundDetailMapper.selectByPage(params, page);
    }

    @Override
    public List<JxcFinanceFundDetailExt> findJxcFinanceFundDetails(Map<String, Object> params) throws Exception {
        return jxcFinanceFundDetailMapper.selectByParams(params);
    }

    @Override
    public void changeJxcFinanceFundDetailAuditState(List<String> sidList, Integer state, SysUserExt opUser) throws Exception {
        JxcFinanceFundDetailExt params = new JxcFinanceFundDetailExt();
        params.setDr(0);
        params.setSid_list(sidList);
        List<JxcFinanceFundDetailExt> detailExtList = jxcFinanceFundDetailMapper.selectByEntity(params);
        if (HlpUtils.isEmptyList(detailExtList)) {
            throw new HlpException("资金信息为空");
        }

        List<JxcFinanceFundDetailExt> filterList = ListUtil.filter(detailExtList, t -> Constants.VALUE_YES == t.getPay_req_state());
        if (!HlpUtils.isEmptyList(filterList)) {
            List<String> codeList = ListUtil.map(filterList, JxcFinanceFundDetailExt::getCode);
            throw new HlpException("单据：" + StringUtils.join(codeList, "、") + "已生成对账，无法" + (Constants.VALUE_YES == state ? "审核" : "反审"));
        }

        List<JxcFinanceFundDetailExt> updateList = new ArrayList<>();
        for (JxcFinanceFundDetailExt detailExt : detailExtList) {
            if (state == 1) {
                detailExt.setAudit_state(Constants.VALUE_YES);
                detailExt.setAuditor_sid(opUser.getSid());
                detailExt.setAuditor_name(opUser.getName());
                detailExt.setAudit_date(new Date());
            } else if (state == -1) {
                detailExt.setAudit_state(Constants.VALUE_NO);
                detailExt.setAuditor_sid(null);
                detailExt.setAuditor_name(null);
                detailExt.setAudit_date(null);
            }
            updateList.add(detailExt);
        }
        if (!HlpUtils.isEmptyList(updateList)) {
            ThreadPoolTaskUtil.executeInList4Split(updateList, false, itemList -> jxcFinanceFundDetailMapper.updateList(itemList));
        }
    }

    @Override
    public void cancelJxcFinanceFundDetail(List<String> sidList, SysUserExt opUser) throws Exception {
        JxcFinanceFundDetailExt params = new JxcFinanceFundDetailExt();
        params.setDr(0);
        params.setSid_list(sidList);
        List<JxcFinanceFundDetailExt> detailExtList = jxcFinanceFundDetailMapper.selectByEntity(params);
        if (HlpUtils.isEmptyList(detailExtList)) {
            throw new HlpException("单据信息为空");
        }

        List<JxcFinanceFundDetailExt> filterList = ListUtil.filter(detailExtList, t -> Constants.VALUE_NO == t.getState());
        if (!HlpUtils.isEmptyList(filterList)) {
            List<String> codeList = ListUtil.map(filterList, JxcFinanceFundDetailExt::getCode);
            throw new HlpException("单据：" + StringUtils.join(codeList, "、") + "已作废");
        }

        filterList = ListUtil.filter(detailExtList, t -> Constants.VALUE_YES == t.getPay_req_state());
        if (!HlpUtils.isEmptyList(filterList)) {
            List<String> codeList = ListUtil.map(filterList, JxcFinanceFundDetailExt::getCode);
            throw new HlpException("单据：" + StringUtils.join(codeList, "、") + "已生成对账，无法作废");
        }

        List<JxcFinanceFundDetailExt> updateList = new ArrayList<>();
        for (JxcFinanceFundDetailExt detailExt : detailExtList) {
            detailExt.setState(Constants.VALUE_NO);
            super.autoInjectBaseData(detailExt, opUser, this.TYPE_UPDATE);
            updateList.add(detailExt);
        }
        if (!HlpUtils.isEmptyList(updateList)) {
            ThreadPoolTaskUtil.executeInList4Split(updateList, false, itemList -> jxcFinanceFundDetailMapper.updateList(itemList));
        }
    }
}
